Repository navigation
fix(libSceFont): stop a font writing from owning its string's characters - #11521
Conversation
MarcZX10
left a comment
There was a problem hiding this comment.
The change removes ownership tied to opaque caller memory, which currently leaks each CharacterStorage when a caller initializes a fresh SceFontWriting; the new test exercises repeated fresh writings, copied writing state and failed initialization. The second error path now clears storage, preventing a failed writing from stepping stale state. The PR reports the focused guest_font test passing on the change and failing on main at the intended leak/copy checks, plus an ASan comparison showing the main-branch leak and copied-writing use-after-free.
There is an explicit lifetime caveat: after sceFontDestroyString, the writing's character storage is freed, so stepping that writing or using a previously returned character pointer is a host use-after-free. I found no evidence in this review that titles do so, and the PR records that PS5 behavior and title call order are unmeasured. The PS4 measurement and Kyty reference support storing writing state in caller memory and treating it as non-owning; this approval does not claim PS5 confirmation. Conventions, diff checks, eligibility, intake and labels pass; hosted product and sanitizer jobs are skipped. I could not run the guest_font target in my local relinker-only scratch build.
|
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
|
|
Manual follow-up to the overlap bot report at the current heads: #11521 changes |
|
@Lindamea1 thanks for the careful analysis; the leak and the copied-writing double release on main are real. The fix moves the problem to the host, though. After Two designs keep your leak fix:
Please add tests that step a writing after destroying its string, including a copied writing and a re-init, and check the chosen outcome; and include an ASan and leak run of |
sceFontWritingInit took a reference on the string's host character storage and kept it in the caller's SceFontWriting bytes. Only a later sceFontWritingInit on the same, unchanged bytes dropped it, so every string drawn with a fresh, zeroed or overwritten writing leaked its characters, and re-initialising both copies of a copied writing dropped its one reference twice. A SceFontWriting has no teardown export, so it is plain caller memory and cannot own anything. sceFontCreateString now allocates the characters through the string's FontMemory, as it already did the string, so no character pointer the guest receives points into host memory. sceFontDestroyString clears the string's magic and frees both, and a failed create clears it before freeing the string. A writing keeps only a pointer to its string and checks the magic on every step, so after the destroy its steps return null unless that memory holds the magic again, as when a new string is allocated there. An init that fails after resetting the writing still leaves it stepping nothing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
798a929 to
ac682a4
Compare
|
@oneandonlydean Thanks. I took your first design and pushed it as ac682a4, rebased onto
New test,
The leak check from the previous push now also requires the interface's allocation count to return to where it was after its 512 draws. Controls, each with only ASan and leaks: in a
The description and the debt entry are updated. The entry now says what the steps and a second destroy return here after a destroy, and that the console's results and where it allocates the characters are not measured. |
|
Windows run at this PR's exact head Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, All seven font tests pass in each of 3 runs, with no failed, skipped or not-run tests:
This is an ordinary Release build. LeakSanitizer isn't available with this toolchain, so the leak itself wasn't measured on Windows. No title, console or negative control was run. AI-assisted: yes (Claude Code). |
What
On
main,sceFontWritingInitmakes the caller'sSceFontWritingown a reference to the string's hostCharacterStorage(Text.cpp:649-650:refCount.fetch_add,ownsStorage = true). Only the nextsceFontWritingIniton the same, unchanged bytes gives it back, by reading the writing's previous contents (Text.cpp:643).sceFontDestroyStringdrops only the string's own reference.A
SceFontWritingis 256 bytes of caller memory (FontTypes.hpp:328-330), and none of the five writing exports (Api.hpp:254-258) tears one down. A guest may zero, overwrite or copy it. Onmainthis means:CharacterStorage(32 bytes,Text.cpp:331) and its characters (200 bytes each,Text.cpp:345). The existingguest_fonttest leaks this way, and LeakSanitizer reports it.Text.cpp:92). In a Release build, doing this twice makes glibc abort withmalloc(): unaligned tcache chunk detected.With this change, a string's characters live in the string's
FontMemory, and a writing only points at its string:sceFontCreateStringcollects the characters in a temporary vector, then allocates them through the string'sFontMemory, as it already allocated the string itself, and copies and links them there.CharacterStorageand its reference count are gone, so the character pointers the guest receives, fromsceFontStringRefersTextCharacters, fromsceFontWritingRefersRenderStepCharacterand through the characters' links, point into memory from the guest's own allocator. If that allocation fails, the string is released and the call returnsSCE_FONT_ERROR_ALLOCATION_FAILED, as when the string's own allocation fails.sceFontDestroyStringclears the string's magic, then frees the characters and the string through the interface, asDestroyWordsdoes with its nodes (Words.cpp:270-280). AsceFontCreateStringthat fails also clears the magic before it releases the string, so no string block this library frees keeps it.sceFontWritingInitno longer reads the writing's previous bytes.sceFontWritingRefersRenderStepandsceFontWritingRefersRenderStepCharactercheck the string's magic before they read the string, so they return null once it is destroyed, with the exception under Behaviour change.main.Behaviour change, after
sceFontDestroyString:maina writing keeps stepping, because it holds a reference, and character pointers taken from the string stay valid while one does. With this change, both step calls return null, on the writing and on any copy of it, unless the destroyed string's memory holds the string magic again; then they read that memory as a string. A string created between the destroy and the step is likely to land there:sceFontDestroyStringfrees the string's block last, and an allocator that hands out the most recently freed block of a size class first, as the guest heap does (GuestHeap.cpp:84-113), is likely to give that block to the next string. Character pointers taken from the string point into the freed characters. Because the string and its characters come from the guest's allocator, the library no longer frees host memory that a writing or a character pointer refers to.sceFontDestroyStringthrough another copy of the handle returnsSCE_FONT_ERROR_INVALID_STRINGinstead of freeing the string again, with the same exception, in which case it frees that memory as a string.Functionalentry indocs/dev/debt/libSceFont.mdsays so.Reference. libSceFont is a Sony library, so FreeBSD has no counterpart:
libSceFont.sprx: the letter step a writing returns is stored inside the caller'sSceFontWriting, so the library keeps writing state in caller memory. The PS5 library was not measured.FontWritingInit, L1815-L1856, L1830), and itsFontDestroyStringfrees the string unconditionally. As a reference, it backs only the non-owning pointer to the string.Open.cpp:46) and writing lines (WritingLine.cpp:238) are already allocated through the caller's memory interface, andDestroyWordsandsceFontCloseFontclear the magic before they free (Words.cpp:275,Open.cpp:473).font.cppL284-L308). Neither that change nor its pull request gives a reason for it.Callers, both PS1 Classics:
sceFontWritingRefersRenderStepCharacterreturns through its pointer argument (fix(libSceFont): return the letter step of sceFontWritingRefersRenderStepCharacter through a pointer #3697).sceFontWritingInitandsceFontWritingRefersRenderStepfill.Neither discussion reports memory growth. The only stop reported, in #9516, is the documented missing-system-fonts case. #3839 reports its title playable, and #9516, with the system fonts and #5209, reaches the first mission. I ran neither title. How often they draw, whether they reuse their writing, and whether they step a writing after destroying its string, is not measured.
Overlap: #5209 changes
FillWritingMetricsFromGlyphinText.cpp. A three-way merge of its head (6912742) with this branch mergesText.cppcleanly, and the merged file passesg++ -fsyntax-onlywith the build's flags. #5209 already conflicts withmainindocs/dev/debt/libSceFont.md, with or without this branch. The overlap bot also lists #8250 (18e0b85), which changes onlydocs/dev/debt/libSceFont.md. It already conflicts withmainthere, near the top of the file, and merged with this branch it conflicts in the same place, away from this entry. #11618 and #11707 add checks toGuestFont.cpp; each merges cleanly with this branch.Regression tests, in
guest_font(core/libs/tests/GuestFont.cpp):CheckWritingDoesNotOwnCharactersdraws a 32-character string 512 times, each time with a fresh writing, and checks the step count. The memory interface's allocation count must come back to where it was, and with glibcmallinfo2().uordblksmust grow by less than 256 KiB. It copies an initialised writing and steps both copies, destroys the string, and re-initialises and steps both copies on a new string. It re-initialises a writing with a character of another string, expectsSCE_FONT_ERROR_INVALID_PARAMETER, and checks that the writing then steps nothing.CheckWritingAfterStringDestroyeduses its ownFontMemory, whose release marks the block as released but keeps it allocated, so that reading a released block stays inside memory the test owns, as reading freed guest memory would in a title. It checks:SCE_FONT_ERROR_ALLOCATION_FAILEDand releases the string, andsceFontDestroyStringon a handle to that released block returnsSCE_FONT_ERROR_INVALID_STRINGwithout a second release;sceFontDestroyStringthe string's and the characters' blocks have been released through the interface, and on the writing and on the copysceFontWritingRefersRenderStepCharacterreturns null and sets the letter step to null, andsceFontWritingRefersRenderStepreturns null;SCE_FONT_ERROR_INVALID_STRING, and the copy still steps nothing;SCE_FONT_ERROR_INVALID_STRINGwithout a second release;Tested
Gentoo Linux, kernel 7.2.8, GCC 16.2.1, AMD Ryzen 7 9800X3D, Release,
BUILD_TESTING=ON, onmain8df2c3e.ctest --test-dir build -R '^guest_font$'passes. Controls, each with onlyText.cppchanged and rebuilt:main'sText.cpp: aborts withFont check failed at line 179, the heap check.Text.cppof this PR's previous head (798a929): aborts at line 231, because no characters are allocated through the memory interface.sceFontDestroyString: aborts at line 249, because the writing still returns its step character after the destroy.sceFontCreateString: aborts at line 74, where the test's release finds the failed create's block released a second time.writing->string = nullptr;on the out-of-range return: aborts at line 197, the failed-init check.-DANYPS5_ENABLE_ASAN=ONRelease tree,guest_font_testsand the libraries it links (libSceFont,libSceFontFt,libc,libkernel) were rebuilt at this head.ASAN_OPTIONS=detect_leaks=1 ctest --test-dir build-asan -R '^guest_font$'passes with no report. Withmain'sText.cppandGuestFont.cppit fails with LeakSanitizer's report of a direct leak of 32 bytes atText.cpp:331and an indirect leak of 400 bytes atText.cpp:345, both fromGuestFont.cpp:658.probe_writing.cpp, attached below). It runs the same create, init, step and destroy cycle as the test against the Release libraries, after one warm-up draw. Unlike the test, it draws an alternatingAVAV...string in a minimal font without akerntable, at 32 px, with a new text source for each draw. For 512 draws of a 32-character string:maingrows by 3,309,568 bytes and leaves 1,024 host allocations live; this change grows by 0 bytes and leaves none;maingrows by 6,512 bytes; this change by 0;maingrows by 3,309,568 bytes; this change by 0.mainand with this change.mainwithmalloc(): unaligned tcache chunk detected(exit 134). With this change it runs 100 times.0x80460002, and the writing then steps 0 times, both onmainand with this change.mainand 0 times with this change. The probe's release callsfree, so with this change that read is of freedmallocmemory;CheckWritingAfterStringDestroyeduses its own release so that this case reads only memory the test still owns.probe_after_destroy.cpp(attached below) steps a writing, or asks for its step character, after destroying its string. Built against the ASan tree: onmainit steps 31 more times, or gets a non-null character, and ASan reports no use-after-free. With this change, ASan reports a heap-use-after-free READ of size 2 inGetStringData(Text.cpp:73), called from either step call: the read of the destroyed string's magic, in the block that the probe's ownAllocatereturned tosceFontCreateStringand itsReleasefreed insceFontDestroyString. The probe's release callsfree, so ASan sees that freed guest-allocator block.tools/select_tests.pypicked 458 of the 2506 tests for this change. All pass, none skipped.Text.cppandGuestFont.cppcompile without warnings withx86_64-w64-mingw32-g++16.2.0 (-std=gnu++20 -fsyntax-only -Wall -Wextra). This is a syntax check, not a Windows build or run. The heap check compiles out there.g++ -std=gnu++20 -fsyntax-only -Wall -Wextraon both files: no warnings.tools/check_compile.py,tools/check_test_registration.pyandtools/check_conventions.pypass.main5bc8403 before this push (256 commits, none in these three files). Thereguest_fontpasses in the Release tree, and in the ASan tree withdetect_leaks=1and no report;tools/select_tests.pypicks 465 of the 2536 tests, all passing; and the overlap checks above give the same results.Not tested: no Windows or macOS build or run, no console, and I ran neither PPSA06699 nor PPSA06323.
Probe sources and output
probe_writing.cppis built from the repository root against the Release tree:Output, without the libraries' log lines. On
main:With this change:
probe_after_destroy.cppis built the same way against the ASan tree, with-fsanitize=address -fno-omit-frame-pointeradded, and run with that tree'slibSceFont.prxbuilt frommain'sText.cppand from this change's. Its argument isstep, or anything else to ask for the step's character:On
mainthe runs usedASAN_OPTIONS=detect_leaks=0; with leak detection on, LeakSanitizer reports the string's leaked storage at exit.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code